<template>
  <div class="app-container">
    <el-form :model="query" :inline="true">
      <el-form-item label="菜单名称">
        <el-input v-model="query.name" placeholder=" "></el-input>
      </el-form-item> 
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="fetchData">查询</el-button>
        <el-button icon="el-icon-refresh" class="filter-item" @click="reload">重置</el-button>
        <!-- 新增的是一级数据 -->
        <el-button type="primary" icon="el-icon-circle-plus-outline" @click="handleAdd(0)">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="list" style="width: 100%" border stripe row-key="id">
      <el-table-column label="序号" type="index" align="center" width="180"></el-table-column>
      <el-table-column align="center" prop="name" label="名称"></el-table-column>

      <el-table-column align="center" prop="url" label="请求地址"></el-table-column>
      <el-table-column align="center" prop="code" label="权限标识"></el-table-column>
      <el-table-column align="center" prop="type" label="类型">
        <!-- 类型(1目录，2菜单，3按钮) -->
        <template slot-scope="scope">
          <span v-if="scope.row.type==1">目录</span>
          <span v-if="scope.row.type==2">菜单</span>
          <span v-if="scope.row.type==3">按钮</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="icon" label="图标">
        <template slot-scope="scope">
          <i :class="scope.row.icon"></i>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="sort" label="排序"></el-table-column>

      <el-table-column align="left" label="操作" width="250">
        <template slot-scope="scope">
          <!-- 新增的是多级数据 -->
          <el-button type="primary" size="small " @click="handleAdd(scope.row.id)">新增</el-button>
          <el-button type="success" size="small " @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button type="danger" size="small " @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <edit
      :visible="edit.visible"
      :title="edit.title"
      :formData="edit.formData"
      :remoteClose="remoteClose"
    ></edit>
  </div>
</template>
<script>
import menu from "@/api/menu";
import edit from "./edit";

export default {
  components: { edit },
  name: "Menu",
  data() {
    return {
      // 查询条件
      query: {},
      // 数据
      list: [],
      edit: {
        visible: false,
        title: "",
        formData: {}
      }
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      menu.getList(this.query).then(res => {
        console.log(res);
        if (res.code == 20000) {
          this.list = res.data;
        }
      });
    },
    // 重置
    reload() {
      this.query = {};
      this.fetchData();
    },
    // 新增菜单一级数据和多级数据
    handleAdd(id) {
      // id=0是新增的一级菜单 否则是新增的某菜单下的子菜单
      this.edit.formData.parentId = id;
      this.edit.visible = true;
      this.edit.title = "新增";
    },
    // 关闭窗口
    remoteClose() {
      this.edit.formData = {};
      this.edit.visible = false;
      this.fetchData();
    },
    // 编辑菜单
    handleEdit(id) {
      menu.getById(id).then(response => {
        if (response.code === 20000) {
          this.edit.formData = response.data;
          //设置标题
          this.edit.title = "编辑";
          this.edit.visible = true;
        }
      });
    },
    handleDelete(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          menu.deleteById(id).then(res => {
            if (res.code == 20000) {
              this.$message({
                type: "success",
                message: "删除成功!"
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>